package com.deepoove.poi.tl.example;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

/**
 * 付款通知书 明细表格的自定义渲染策略<br/>
 * 1. 填充货品数据 <br/>
 * 2. 填充人工费数据 <br/>
 *
 * @author Sayi
 */
public class DetailTablePolicy1 extends DynamicTableRenderPolicy {

    // 货品填充数据所在行数
    int goodsStartRow = 2;
    // 人工费填充数据所在行数
    int laborsStartRow = 5;

    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        if (null == data) return;
        DetailData detailData = (DetailData) data;

        List<RowRenderData> labors = detailData.getLabors();
        if (null != labors) {
            table.removeRow(laborsStartRow);
            // 循环插入行
            for (int i = 0; i < labors.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(laborsStartRow);
                for (int j = 0; j < 7; j++) insertNewTableRow.createCell();

                // 合并单元格
                if (i == 0) {
                    TableTools.mergeCellsHorizonal(table, laborsStartRow, 0, 3);
                }
                if (i == 1) {
                    TableTools.mergeCellsHorizonal(table, laborsStartRow, 1, 4);
                }
                if (i == 2) {
                    TableTools.mergeCellsHorizonal(table, laborsStartRow, 2, 5);
                }
                TableRenderPolicy.Helper.renderRow(table.getRow(laborsStartRow), labors.get(i));
            }
        }

//        List<RowRenderData> goods = detailData.getGoods();
//        if (null != goods) {
//            table.removeRow(goodsStartRow);
//            for (int i = 0; i < goods.size(); i++) {
//                XWPFTableRow insertNewTableRow = table.insertNewTableRow(goodsStartRow);
//                for (int j = 0; j < 7; j++) insertNewTableRow.createCell();
//                TableRenderPolicy.Helper.renderRow(table.getRow(goodsStartRow), goods.get(i));
//            }
//        }
    }

}
